package com.vrv.app.cyberspace.modules.sys.controller;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.vrv.app.cyberspace.common.ddio.ApiV2Service;
import com.vrv.app.cyberspace.common.utils.HttpContextUtils;
import com.vrv.app.cyberspace.common.utils.JSONResult;
import com.vrv.app.cyberspace.common.validator.Assert;
import com.vrv.app.cyberspace.common.web.RoleBean;
import com.vrv.app.cyberspace.modules.sys.entity.*;
import com.vrv.app.cyberspace.modules.sys.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

import static com.vrv.app.cyberspace.common.utils.JSONResult.renderError;
import static com.vrv.app.cyberspace.common.utils.JSONResult.renderSuccess;

/**
 * @Description 
 * @Author by sm
 * @Create 2018-01-24 12:08
 */
@RestController
@RequestMapping("/manage")
public class ManageController extends BaseController {
    @Autowired
    private IManageUserTableService manageUserTableService;
    @Autowired
    private IIndustryTableService industryTableService;
    @Autowired
    private IInformationTypeTableService informationTypeTableService;
    @Autowired
    private IUserTableService userTableService;
    @Autowired
    private ApiV2Service apiV2Service;
    @Autowired
    private IRegionTableService regionTableService;
    @Autowired
    private IRiskTypeTableService riskTypeTableService;
    @Autowired
    private IEventTypeTableService eventTypeTableService;
    @Autowired
    private IRoleTableService roleTableService;
    @Autowired
    private IOrganizeTableService iOrganizeTableService;

    /**
     * 登陆
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public JSONResult login(String username,String password){
        ManageUserTable manageUserTable = manageUserTableService.selectOne(new EntityWrapper<ManageUserTable>().eq("username",username));
        if (manageUserTable==null){
            return renderError("用户不存在，登陆失败！");
        }
        if (SecureUtil.md5(password).equals(manageUserTable.getPassword())){
            HttpContextUtils.getHttpServletRequest().getSession().setAttribute("manageUser", manageUserTable);
            manageUserTable.setRoleID(0);
            manageUserTable.setPassword(null);
            return renderSuccess(manageUserTable);
        }
        return renderError("密码错误，登陆失败！");
    }

    /**
     * 修改密码
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @PostMapping("/editPassword")
    public JSONResult editPassword(String oldPassword,String newPassword){
        ManageUserTable manageUserTable = (ManageUserTable) HttpContextUtils.getHttpServletRequest().getSession().getAttribute("manageUser");
        if (manageUserTableService.selectOne(new EntityWrapper<ManageUserTable>().eq("username",manageUserTable.getUsername()).eq("password",SecureUtil.md5(oldPassword)))==null){
            return renderError("旧密码错误，密码修改失败！");
        }
        manageUserTable.setPassword(SecureUtil.md5(newPassword));
        manageUserTable.updateById();
        return renderSuccess();
    }

    /**
     * 注销
     * @return
     */
    @PostMapping("/logout")
    public JSONResult logout(){
        HttpContextUtils.getHttpServletRequest().getSession().invalidate();
        return renderSuccess();
    }

    /**
     * 获取行业列表
     * @return
     */
    @GetMapping("/industry")
    public JSONResult getListIndustry() {
        return renderSuccess(industryTableService.selectList(new EntityWrapper<IndustryTable>().orderBy("id",true)));
    }

    /**
     * 增加行业
     * @param name
     * @return
     */
    @PostMapping("/industry")
    public JSONResult addIndustry(String name) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("name",name);
        if (industryTableService.selectByMap(map).size()>0){
            return renderError("数据库中已存在该记录");
        }
        IndustryTable industryTable = new IndustryTable();
        industryTable.setName(name);
        industryTable.insert();
        return renderSuccess();
    }

    /**
     * 更新行业
     * @param id
     * @param name
     * @return
     */
    @PutMapping("/industry/{id}")
    public JSONResult updateIndustry(@PathVariable Integer id,String name) {
        IndustryTable industryTable = new IndustryTable();
        industryTable.setId(id);
        industryTable.setName(name);
        industryTable.updateById();
        return renderSuccess();
    }

    /**
     * 获取地域列表
     * @return
     */
    @GetMapping("/region")
    public JSONResult getListRegion() {
        return renderSuccess(regionTableService.selectList(null));
    }

    /**
     * 增加地域
     * @param name
     * @return
     */
    @PostMapping("/region")
    public JSONResult addRegion(String name) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("name",name);
        if (regionTableService.selectByMap(map).size()>0){
            return renderError("数据库中已存在该记录");
        }
        RegionTable regionTable = new RegionTable();
        regionTable.setName(name);
        regionTable.insert();
        return renderSuccess();
    }

    /**
     * 更新地域
     * @param id
     * @param name
     * @return
     */
    @PutMapping("/region/{id}")
    public JSONResult updateRegion(@PathVariable Integer id,String name) {
        RegionTable regionTable = new RegionTable();
        regionTable.setId(id);
        regionTable.setName(name);
        regionTable.updateById();
        return renderSuccess();
    }

    /**
     * 获取信息类型列表
     * @return
     */
    @GetMapping("/informationType")
    public JSONResult getListInformation(){
        return renderSuccess(informationTypeTableService.selectList(null));
    }

    /**
     * 增加信息类型
     * @param itt
     * @return
     */
    @PostMapping("/informationType")
    public JSONResult addInformation(InformationTypeTable itt){
        informationTypeTableService.insert(itt);
        return renderSuccess();
    }

    /**
     * 更新信息类型
     * @param id
     * @param itt
     * @return
     */
    @PutMapping("/informationType/{id}")
    public JSONResult updateInformation(@PathVariable Integer id,InformationTypeTable itt){
        itt.setId(id);
        informationTypeTableService.updateById(itt);
        return renderSuccess();
    }

    /**
     * 获取风险类型列表
     * @return
     */
    @GetMapping("/riskType")
    public JSONResult getListRiskType(){
        return renderSuccess(riskTypeTableService.selectList(new EntityWrapper<RiskTypeTable>().orderBy("id",true)));
    }

    /**
     * 增加风险类型
     * @param rt
     * @return
     */
    @PostMapping("/riskType")
    public JSONResult addRiskType(RiskTypeTable rt){
        riskTypeTableService.insert(rt);
        return renderSuccess();
    }

    /**
     * 更新风险类型
     * @param id
     * @param rt
     * @return
     */
    @PutMapping("/riskType/{id}")
    public JSONResult updateRiskType(@PathVariable Integer id,RiskTypeTable rt){
        rt.setId(id);
        riskTypeTableService.updateById(rt);
        return renderSuccess();
    }

    /**
     * 获取事件类型列表
     * @return
     */
    @GetMapping("/eventType")
    public JSONResult getListEvent(Integer parentID){
        Assert.isNull(parentID,"level不能为空");
        return eventTypeTableService.getListEventType(parentID);
    }

    /**
     * 增加事件类型
     * @param et
     * @return
     */
    @PostMapping("/eventType")
    public JSONResult addEventType(EventTypeTable et){
        eventTypeTableService.insert(et);
        return renderSuccess();
    }

    /**
     * 更新事件类型
     * @param id
     * @param et
     * @return
     */
    @PutMapping("/eventType/{id}")
    public JSONResult updateEventType(@PathVariable Integer id,EventTypeTable et){
        et.setId(id);
        eventTypeTableService.updateById(et);
        return renderSuccess();
    }

    /**
     * 获取角色列表
     * @return
     */
    @GetMapping("/role")
    public JSONResult getListRole(){
        return renderSuccess(roleTableService.selectList(null));
    }

    /**
     * 增加角色
     * @param re
     * @return
     */
    @PostMapping("/role")
    public JSONResult addRole(RoleTable re){
        re.insert();
        return renderSuccess();
    }

    /**
     * 更新角色
     * @param id
     * @param rt
     * @return
     */
    @PutMapping("/role/{id}")
    public JSONResult updateRole(@PathVariable Integer id,RoleTable rt){
        rt.setId(id);
        rt.insert();
        return renderSuccess();
    }

    /**
     * 获取组织信息
     */
    @RequestMapping("/organizeTable/list")
    public JSONResult getListRegion(Integer orgID) {
        return renderSuccess(iOrganizeTableService.selectList(new EntityWrapper<OrganizeTable>().eq("parentOrgID",orgID)));
    }

    /**
     * 同步组织和人员
     */
    @RequestMapping("/sync")
    public JSONResult syncDDIOUser() {
        return userTableService.synchDDIOUser();
    }

    /**
     * 同步所有组织
     */
    @RequestMapping("/getAllOrg")
    public JSONResult getAllOrg() {
        return renderSuccess(apiV2Service.getAllOrg());
    }

    /**
     * 给人员分配角色
     */
    @RequestMapping("/distribute")
    public JSONResult distributeUserRole(RoleBean roleBean) {
        return userTableService.distributeUserRole(roleBean);
    }
}
